% Figure 3
% The consumer credit risk premium (crisis period omitted)
% These panels plot the average consumer credit risk premium for the A,
% B, and C tranches, respectively. Averages are taken over all A, B, or C
% tranches in the sample for each month.
% clear; clc;



%load data
opts = delimitedTextImportOptions("NumVariables", 8);
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
opts.VariableNames = ["Year", "Month", "SpreadPremium_A", "SpreadPremium_B", "SpreadPremium_C", "Crdspread_A", "Crdspread_B", "Crdspread_C"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double"];
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
file = "IndexCardPremia.csv";
Premia= readtable(file, opts);
clear opts
Premia.Date = eomdate(Premia.Year,Premia.Month,'datetime');
Premia = table2timetable(Premia,'RowTimes','Date');
PlotData=Premia(:,["SpreadPremium_A","SpreadPremium_B","SpreadPremium_C"]);

PlotData_Pre  = PlotData(1:84,:);
PlotData_Post = PlotData(121:end,:);
crisisPeriod = [datenum(PlotData.Date(85)) datenum(PlotData.Date(120))];

fig1 = figure('NumberTitle', 'Off', 'Name', 'Figure 1');

figOrientation = 'portrait';
figWidth = 8.0; 
figHeight = 8.0; 


set(fig1,'Units','inch');
set(fig1,'Position',[0 0 figWidth figHeight]);
movegui(fig1,'center')


set(fig1,'PaperUnits', 'centimeters');
set(fig1,'PaperSize', [figWidth figHeight]);
set(fig1,'PaperPosition',[0 0 figWidth figHeight]);
set(fig1,'PaperOrientation',figOrientation);
set(fig1,'InvertHardcopy','on');


subplot_er(3,1,1);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_1 = plot(datenum(PlotData_Pre.Date),PlotData_Pre.SpreadPremium_A);
set(plot_1, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
hold on;
plot_12 = plot(datenum(PlotData_Post.Date),PlotData_Post.SpreadPremium_A);
set(plot_12, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
hold on
ylim([-80,80])
recessionplot('recessions',crisisPeriod)
hold off
minX = min(datenum(PlotData.Date));
maxX = max(datenum(PlotData.Date));
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2002'; ...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2002';...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylabel('Basis Points')
title('A Tranche','FontWeight','normal')
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);


subplot_er(3,1,2);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_21 = plot(datenum(PlotData_Pre.Date),PlotData_Pre.SpreadPremium_B);
set(plot_21, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
hold on
plot_2 = plot(datenum(PlotData_Post.Date),PlotData_Post.SpreadPremium_B);
set(plot_2, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
ylim([-100,200])
hold on
recessionplot('recessions',crisisPeriod)
hold off
minX = min(datenum(PlotData.Date));
maxX = max(datenum(PlotData.Date));
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2002'; ...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2002';...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylabel('Basis Points')
title('B Tranche','FontWeight','normal')
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);



subplot_er(3,1,3);
ax = gca;
ax.PlotBoxAspectRatio = [1,0.35,0.35];
plot_31 = plot(datenum(PlotData_Pre.Date),PlotData_Pre.SpreadPremium_C);
set(plot_31, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
hold on
plot_32 = plot(datenum(PlotData_Post.Date),PlotData_Post.SpreadPremium_C);
set(plot_32, 'LineStyle', '-', 'Color', 'b' , 'LineWidth', 1.00);
ylim([-150,300])
hold on
recessionplot('recessions',crisisPeriod)
hold off
minX = min(datenum(PlotData.Date));
maxX = max(datenum(PlotData.Date));
xlim([minX-0.025 maxX+0.025])
dateaxis
set(gca,'XTick',datenum([...
    'Jan-1-2002'; ...
    'Jan-1-2006'; ...
    'Jan-1-2010'; ...
    'Jan-1-2014'; ...
    'Jan-1-2018']));
set(gca,'XTickLabel',[...
    '2002';...
    '2006';...
    '2010';...
    '2014';...
    '2018'])
ylabel('Basis Points')
title('C Tranche','FontWeight','normal')
grid 'on';
box 'on';
set(gcf, 'Color', [1,1,1]);

